import { Component, Injectable, Injector, Input, OnInit } from '@angular/core';
import { UniversalFunctionsUtil } from '../../../core/universal-functions.util';
import * as _ from 'lodash';
import { Validators } from '@angular/forms';
import { _HttpClient } from '@delon/theme';
import { DatePipe } from '@angular/common';
import { environment } from '../../../../environments/environment';

@Injectable({ providedIn: 'root' })

@Component({
  selector: 'app-reportform',
  templateUrl: './emergentRepairFrom.component.html',
  styles: [`
      .tac {
        text-align: center;
      }      
      .big-bold {
        font-size: 22px;
        font-weight: 700;
      }      
      .ant-form-item {
        margin-bottom: 0;
      }      
      form .ant-mentions, form textarea.ant-input {
          font-size: 14px;
      }      
      form  textarea {
        font-size: 16px;
      }  
    `],
})
export class EmergentRepairFromComponent extends UniversalFunctionsUtil implements OnInit {
  @Input() apiIdentity;
  @Input() btnType;
  @Input() queryId;
  @Input() readOnly;

  constructor(
    private injector: Injector,
    private http: _HttpClient,
    private datePipe: DatePipe
  ) {
    super(injector);
  }

  fileName = '紧急修天窗的情况汇报.doc';
  submitData = {
    emerDate: null,
    station: ``,
    header: `XX 年 XX 月 XX 日，XX 车间在 XX 站申请紧急天窗 1 个，未影响行车。现将紧急天窗执行情况汇报如下：`,
    devInfo: `XX 年 XX 月 XX 日，XX 车间 XXX 调阅监测发现，XX 站 XX设备发生 XX 问题，为保证设备正常运行，特申请紧急修天窗上道检查。`,
    handleInfo: `XX 月 XX 日 XX 车间申请紧急天窗对 XX 站 XX 设备进行检查，行调发调度命令 XX 号（同意进行紧急修天窗作业，给点时间 X 时 X 分至 X 时 X 分，XX 分钟）。检查人员 XX、XX、XX、其他专业 X 人，共计 X 人。上道检查情况：XXXXXXXXXX;处理情况：XXXXXXXXX。`,
    reason: `XX 站 XX 设备 XXX。为确保信号设备正常运用，申请紧急修天窗对 XX 站 XX 设备进行检查。`,
    punishSuggest: `XX 站 XX 设备 XX，申请紧急修天窗对 XX 站 XX 设备进行检查，未影响行车，建议不考核。`,
    reportDate: null
  };

  showModal() {
    this.isVisible = true;
    const queryId = this.queryId;
    const submitData: any = this.submitData;
    if (queryId) {
      this.submitting = true;
      this.scanReportScuttle({ id: queryId }).subscribe(res => {
        if (!_.isEmpty(res)) {
          submitData.emerDate = res.emerDate || this.today;
          submitData.station = res.station || '';
          submitData.header = res.header || '';
          submitData.devInfo = res.devInfo || '';
          submitData.handleInfo = res.handleInfo || '';
          submitData.reason = res.reason || '';
          submitData.punishSuggest = res.punishSuggest || '';
          submitData.reportDate = res.reportDate || this.today;
          this.submitting = false;
        }
      }, err => this.errorHandle(err));
    }
  }

  

  // 紧急修天窗文件查询
  scanReportScuttle(params) {
    const url = environment.apiBase + '/csmis/emerSkywinreport/getOne.jwt';
    return this.http.get(url, params);
  }

  submitForm(params) {
    params.emerDate = this.datePipe.transform(params.emerDate, 'yyyy-MM-dd');
    params.reportDate = this.datePipe.transform(params.reportDate, 'yyyy-MM-dd');
    this.btnType == 'default' && this.queryId ? params.id = this.queryId : params.id = '';

    const formData = new FormData();
    formData.append('devInfo', params.devInfo);
    formData.append('emerDate', params.emerDate);
    formData.append('handleInfo', params.handleInfo);
    formData.append('header', params.header);
    formData.append('id', params.id);
    formData.append('punishSuggest', params.punishSuggest);
    formData.append('reason', params.reason);
    formData.append('reportDate', params.reportDate);
    formData.append('station', params.station);

    this.reportScuttleForm(formData).subscribe(res => {
      this.remoteMessage(res);
      this.ums.sendMessage('reportrepair' as string);
      this.submitting = this.isVisible = false;
    }, err => this.errorHandle(err));
  }

  // 紧急修天窗文件提交
  reportScuttleForm(params) {
    const url = environment.apiBase + '/csmis/emerSkywinreport/save.jwt';
    return this.http.post(url, params);
  }

  ngOnInit(): void {
    const submitData = this.submitData;
    submitData.reportDate = submitData.emerDate = this.today;
    this.validateForm = this.fb.group({
      emerDate: [submitData.emerDate, [Validators.required]],
      station: [submitData.station, [Validators.required], [this.customizedValidator]],
      header: [submitData.header, [Validators.required], [this.customizedValidator]],
      devInfo: [submitData.devInfo, [Validators.required], [this.customizedValidator]],
      handleInfo: [submitData.handleInfo, [Validators.required], [this.customizedValidator]],
      reason: [submitData.reason, [Validators.required], [this.customizedValidator]],
      punishSuggest: [submitData.punishSuggest, [Validators.required], [this.customizedValidator]],
      reportDate: [submitData.reportDate, [Validators.required]],
    });
  }

}


